home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: Auge 4000 / Auge 4000 #06 (1987-05-23)(Amiga User Gruppe Einzugsgebiet 4000).zip / Auge 4000 #06 (1987-05-23)(Amiga User Gruppe Einzugsgebiet 4000).adf / Filemaper / Filemaper_source < prev    next >
C/C++ Source or Header  |  1987-03-08  |  8KB  |  355 lines

  1. /*  :ts=8 bk=0
  2.  * File mapper.  Uses trackdisk.device to grab sectors and traverse the
  3.  * filesystem the hard way to find out what sectors a particular file
  4.  * occupies.
  5.  *
  6.  * Crufted together by Leo Schwab while waiting for an open line on the WELL.
  7.  *    8608.19
  8.  * Finally finished:    8609.16
  9.  * I could have done it quicker if I hadn't started working for Wendy's.
  10.  *
  11.  * Note: This program is only valid for 3.5" floppy drives.
  12.  */
  13.  
  14. #include <exec/types.h>
  15. #include <exec/memory.h>
  16. #include <intuition/intuition.h>
  17. #include <libraries/dos.h>
  18. #include <devices/trackdisk.h>
  19. #include "things.h"
  20.  
  21.  
  22. extern struct NewWindow    windef;
  23. extern struct IntuiText    errmsg, ok;
  24. extern struct Gadget    gad[];
  25. extern char        filename[], devname[];
  26.  
  27. struct Window    *win;
  28. struct RastPort *rp;
  29. struct InfoData *id;
  30. struct IOExtTD    *diskreq;
  31. struct MsgPort    *diskport;
  32. ULONG        diskchangecount, *diskbuffer, bitmap[SIZE];
  33. int        bmsect;
  34. void        *IntuitionBase, *GfxBase, *lok;
  35.  
  36.  
  37. main (ac, av)
  38. char *av[];
  39. {
  40.     struct IntuiMessage *msg;
  41.     struct Gadget *ptr;
  42.     int class;
  43.  
  44.     if (ac) {
  45.         ac--;  av++;
  46.         if (ac) {
  47.             strcpy (devname, *av);
  48.             ac--;  av++;
  49.         } else
  50.             strcpy (devname, "df0:");
  51.  
  52.         if (ac)
  53.             strcpy (filename, *av);
  54.         else
  55.             strcpy (filename, ":");
  56.     }
  57.     openstuff ();
  58.     drawgrid ();
  59.     setdev ();
  60.  
  61.     /*  Process IntuiEvents  */
  62.     for ever {
  63.         WaitPort (win -> UserPort);
  64.         msg = GetMsg (win -> UserPort);
  65.         class = msg -> Class;
  66.         ptr = (struct Gadget *) msg -> IAddress;
  67.         ReplyMsg (msg);
  68.         if (class == CLOSEWINDOW)
  69.             break;
  70.         else if (class == GADGETUP) {
  71.             if (ptr == gad) {        /*  New filename  */
  72.                 drawbitmap ();
  73.                 findfile (filename);
  74.             } else if (ptr == &gad[1])    /*  New device  */
  75.                 setdev ();
  76.             else if (ptr == &gad[2]) {    /*  Refresh screen  */
  77.                 getbitmap ();
  78.                 drawbitmap ();
  79.                 findfile (filename);
  80.             }
  81.         }
  82.     }
  83.     closestuff ();
  84. }
  85.  
  86.  
  87. setdev ()
  88. {
  89.     int unit;
  90.  
  91.     /*
  92.      * This rather lengthy series of DOS calls is needed to turn DOS
  93.      * device names into unit numbers that OpenDevice() can deal with.
  94.      */
  95.     if (!(lok = Lock (devname, ACCESS_READ)))
  96.         die ("Can't obtain lock for specified device.");
  97.  
  98.     if (!(id = AllocMem ((long) sizeof (*id), MEMF_CLEAR)))
  99.         die ("Can't get InfoData memory.");
  100.  
  101.     if (!Info (lok, id))
  102.         die ("Call to Info() failed.");
  103.  
  104.     if (id -> id_DiskType == ID_NO_DISK_PRESENT)
  105.         die ("No disk in drive.");
  106.  
  107.     unit = id -> id_UnitNumber;
  108.     FreeMem (id, (long) sizeof (*id));  id = NULL;
  109.     UnLock (lok);  lok = NULL;
  110.     opendisk (unit);
  111.     getbitmap ();
  112.     drawbitmap ();
  113. }
  114.  
  115. getbitmap ()
  116. {
  117.     register int i;
  118.  
  119.     MotorOn ();
  120.     GetSector ((long) ROOTBLOCK);
  121.     bmsect = diskbuffer [BITMAPINDEX];
  122.     GetSector ((long) bmsect);
  123.     MotorOff ();
  124.     for (i=0; i<SIZE; i++)
  125.         bitmap [i] = diskbuffer [i];
  126. }
  127.  
  128. drawbitmap ()
  129. {
  130.     register int i, n, l, p;
  131.     int free = NUMBLOCKS-2;
  132.     long k, x, y;
  133.     char buf[80];
  134.  
  135.     SetDrMd (rp, JAM1);
  136.     SetAPen (rp, 3L);
  137.  
  138.     /*  Show sectors 0 and 1 (always allocated)  */
  139.     RectFill (rp, XOFF+1, YOFF+1, XX+XOFF-1, YY+YY+YOFF-1);
  140.     l = 2;
  141.     for (i=1; i<=NUMLONGS; i++) {
  142.         k = bitmap[i];
  143.         for (n=0; n<32; n++) {
  144.             /*  Bits progress from low to high order  */
  145.             if (i<NUMLONGS || n<30) {    /*  Ignore last two  */
  146.                 /*  Perform icky conversion  */
  147.                 x = (l / 22) * XX + XOFF;
  148.                 y = (l % 22) * YY + YOFF;
  149.                 if (y >= BRKOVER)
  150.                     y += SEP;
  151.  
  152.                 /*
  153.                  * The following incantation basically means,
  154.                  * don't draw sectors that don't need to be
  155.                  * drawn.
  156.                  */
  157.                 p = ReadPixel (rp, x+1, y+1);
  158.                 if (~k & 1) {
  159.                     free--;
  160.                     if (p != 3) {
  161.                         SetAPen (rp, 3L);
  162.                         RectFill (rp, x+1, y+1,
  163.                               x+XX-1, y+YY-1);
  164.                     }
  165.                 } else if (p) {
  166.                     SetAPen (rp, 0L);
  167.                     RectFill
  168.                      (rp, x+1, y+1, x+XX-1, y+YY-1);
  169.                 }
  170.                 k >>= 1;
  171.             }
  172.             l++;    /*  Increment sector number  */
  173.         }
  174.     }
  175.  
  176.     /*  Do labels  */
  177.     SetAPen (rp, 1L);
  178.     SetBPen (rp, 0L);
  179.     SetDrMd (rp, JAM2);
  180.     sprintf (buf, "Bitmap on sector %-4d", bmsect);
  181.     Move (rp, XOFF, LABEL_Y);
  182.     Text (rp, buf, (long) strlen (buf));
  183.     sprintf (buf, "Sectors free: %-4d", free);
  184.     Move (rp, 250L, LABEL_Y);
  185.     Text (rp, buf, (long) strlen (buf));
  186.     sprintf (buf, "Allocated: %-4d", (int) NUMBLOCKS-free);
  187.     Move (rp, 450L, LABEL_Y);
  188.     Text (rp, buf, (long) strlen (buf));
  189.     Move (rp, NUMCYLS*XX+XOFF+10, NUMSECS*YY/2+YOFF+2);
  190.     Text (rp, "Surface 0", 9L);
  191.     Move (rp, NUMCYLS*XX+XOFF+10, NUMSECS*YY/2+BRKOVER+SEP+2);
  192.     Text (rp, "Surface 1", 9L);
  193. }
  194.  
  195. drawgrid ()    /*  Draw grid and labels so we can see  */
  196. {
  197.     long x, y;
  198.  
  199.     SetDrMd (rp, JAM1);
  200.     SetAPen (rp, 1L);
  201.     for (x=XOFF; x<=80*XX+XOFF; x += XX) {
  202.         Move (rp, x, YOFF);
  203.         Draw (rp, x, YOFF+11*YY);
  204.         Move (rp, x, BRKOVER+SEP);
  205.         Draw (rp, x, BRKOVER+SEP+11*YY);
  206.     }
  207.     for (y=0; y<=11*YY; y += YY) {
  208.         Move (rp, XOFF, y+YOFF);
  209.         Draw (rp, XOFF+80*XX, y+YOFF);
  210.         Move (rp, XOFF, y+SEP+BRKOVER);
  211.         Draw (rp, XOFF+80*XX, y+SEP+BRKOVER);
  212.     }
  213.  
  214.     /*  Draw map markings  */
  215.     Move (rp, XOFF+XX/2, YOFF);      Draw (rp, XOFF+XX/2, YOFF-3);
  216.     Move (rp, XOFF+80*XX-XX/2, YOFF); Draw (rp, XOFF+80*XX-XX/2, YOFF-3);
  217.     Move (rp, XOFF, YOFF+YY/2);      Draw (rp, XOFF-3, YOFF+YY/2);
  218.     Move (rp, XOFF, YOFF+YY*10+YY/2); Draw (rp, XOFF-3, YOFF+YY*10+YY/2);
  219.     Move (rp, XOFF-1, YOFF-4);      Text (rp, "0", 1L);
  220.     Move (rp, XOFF+79*XX-1, YOFF-4);  Text (rp, "79", 2L);
  221.     Move (rp, XOFF-12, YOFF+6);      Text (rp, "0", 1L);
  222.     Move (rp, XOFF-20, YOFF+11*YY);   Text (rp, "10", 2L);
  223. }
  224.  
  225. marksector (n, color)
  226. long n;
  227. int color;
  228. {
  229.     register int x, y;
  230.  
  231.     x = (n / 22) * XX + XOFF;
  232.     y = (n % 22) * YY + YOFF;
  233.     if (y >= BRKOVER)
  234.         y += SEP;
  235.     SetAPen (rp, (long) color);
  236.     RectFill (rp, x+1L, y+1L, x+XX-1L, y+YY-1L);
  237. }
  238.  
  239. openstuff ()
  240. {
  241.     if (!(IntuitionBase = OpenLibrary ("intuition.library", REV))) {
  242.         /*
  243.          * If we can't open Intuition, then we can't use
  244.          * AutoRequest ()
  245.          */
  246.         printf ("Intuition failed; you'll have to use logic.\n");
  247.         closestuff ();
  248.         exit (100);
  249.     }
  250.  
  251.     if (!(GfxBase = OpenLibrary ("graphics.library", REV))) {
  252.         printf ("Art shop closed.\n");
  253.         closestuff ();
  254.         exit (100);
  255.     }
  256.  
  257.     if (!(win = OpenWindow (&windef))) {
  258.         printf ("Window painted shut.\n");
  259.         closestuff ();
  260.         exit (100);
  261.     }
  262.     rp = win -> RPort;
  263.  
  264.     if (!(diskport = CreatePort (NULL, NULL)))
  265.         die ("No port.");
  266.  
  267.     if (!(diskreq = CreateExtIO (diskport, (long) sizeof (*diskreq))))
  268.         die ("Can't make IO block.");
  269.  
  270.     if (!(diskbuffer = AllocMem (BLOCKSIZE, MEMF_CLEAR | MEMF_CHIP)))
  271.         die ("Can't allocate disk buffer.");
  272. }
  273.  
  274. opendisk (unit)
  275. int unit;
  276. {
  277.     long err;
  278.     char *buf[80];
  279.  
  280.     /*  We may be changing units, so close it if it's open  */
  281.     if (diskreq -> iotd_Req.io_Device) {
  282.         CloseDevice (diskreq);
  283.         diskreq -> iotd_Req.io_Device = NULL;
  284.     }
  285.  
  286.     if (err = OpenDevice (TD_NAME, (long) unit, diskreq, NULL)) {
  287.         sprintf (buf, "Can't get at disk; err = %ld.", err);
  288.         die (buf);
  289.     }
  290.  
  291.     diskreq -> iotd_Req.io_Command = TD_CHANGENUM;
  292.     DoIO (diskreq);
  293.     diskchangecount = diskreq -> iotd_Req.io_Actual;
  294. }
  295.  
  296. closestuff ()
  297. {
  298.     if (lok)
  299.         UnLock (lok);
  300.     if (diskreq) {
  301.         /*
  302.          * Apparently, if OpenDevice() fails, it fills in the
  303.          * io_Device field with -1.  This pretty much blows all
  304.          * my previous code out of the water, which assumed it got
  305.          * filled in with 0.  Sigh.  Why don't they tell us these
  306.          * things?
  307.          */
  308.         if ((long) diskreq -> iotd_Req.io_Device != -1L)
  309.             CloseDevice (diskreq);
  310.         DeleteExtIO (diskreq, (long) sizeof (*diskreq));
  311.     }
  312.  
  313.     if (diskbuffer)
  314.         FreeMem (diskbuffer, (long) BLOCKSIZE);
  315.     if (id)
  316.         FreeMem (id, (long) sizeof (*id));
  317.     if (diskport)
  318.         DeletePort (diskport);
  319.     if (win)
  320.         CloseWindow (win);
  321.     if (GfxBase)
  322.         CloseLibrary (GfxBase);
  323.     if (IntuitionBase)
  324.         CloseLibrary (IntuitionBase);
  325. }
  326.  
  327. notice (str)    /*  For non-fatal errors  */
  328. UBYTE *str;
  329. {
  330.     MotorOff ();
  331.     errmsg.IText = str;
  332.     AutoRequest (win, &errmsg, NULL, &ok, NULL, NULL,
  333.              TextLength (rp, str, (long) strlen (str)) + 40, 46L);
  334. }
  335.  
  336. die (str)    /*  For fatal errors  */
  337. UBYTE *str;
  338. {
  339.     errmsg.IText = str;
  340.     AutoRequest (win, &errmsg, NULL, &ok, NULL, NULL,
  341.              TextLength (rp, str, (long) strlen (str)) + 40, 46L);
  342.     closestuff ();
  343.     exit (100);
  344. }
  345.  
  346. /*    Guess....
  347. debug (str)
  348. char *str;
  349. {
  350.     printf (str);
  351.     getchar ();
  352.  
  353. }
  354. */
  355.